/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Auction.java
 *
 * Created on 15.05.2009, 14:45:40
 */

package Tabs;

import Binding.Binder;
import BusinessLayer.BusinessLayer;
import Objekte.AuctionListModel;
import Objekte.AuctionProdukt;
import logging.Logger;
import wisi.WiSiView;

/**
 *
 * @author benji
 */
public class Auction extends javax.swing.JPanel {
    private Binder binder = new Binder();

    private AuctionListModel auctionModel;

    /** Creates new form Auction */
    public Auction() {
        initComponents();

        Logger.debug("Lade " + this.getName());

        auctionModel = new AuctionListModel();
        this.jTable1.setModel(auctionModel);
    }

    /**
     * AuktionListe laden
     */
    public void BindTo() {
        try{
            if(!BusinessLayer.isLoggedIn())return;
            auctionModel.setData(BusinessLayer.getAuctionLItemist());
            Logger.debug("BindTo");
        }
        catch(Exception e){
            Logger.error("BindTo - AuctionTab", e);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        auctionIdLabel = new javax.swing.JLabel();
        productLabel = new javax.swing.JLabel();
        mengeLabel = new javax.swing.JLabel();
        veranstalterLabel = new javax.swing.JLabel();
        bieterLabel = new javax.swing.JLabel();
        gebotLabel = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        bidSpinner = new javax.swing.JSpinner();
        jLabel9 = new javax.swing.JLabel();
        bidButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wisi.WiSiApp.class).getContext().getResourceMap(Auction.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        auctionIdLabel.setText(resourceMap.getString("auctionIdLabel.text")); // NOI18N
        auctionIdLabel.setName("auctionIdLabel"); // NOI18N

        productLabel.setText(resourceMap.getString("productLabel.text")); // NOI18N
        productLabel.setName("productLabel"); // NOI18N

        mengeLabel.setText(resourceMap.getString("mengeLabel.text")); // NOI18N
        mengeLabel.setName("mengeLabel"); // NOI18N

        veranstalterLabel.setText(resourceMap.getString("veranstalterLabel.text")); // NOI18N
        veranstalterLabel.setName("veranstalterLabel"); // NOI18N

        bieterLabel.setText(resourceMap.getString("bieterLabel.text")); // NOI18N
        bieterLabel.setName("bieterLabel"); // NOI18N

        gebotLabel.setText(resourceMap.getString("gebotLabel.text")); // NOI18N
        gebotLabel.setName("gebotLabel"); // NOI18N

        timeLabel.setText(resourceMap.getString("timeLabel.text")); // NOI18N
        timeLabel.setName("timeLabel"); // NOI18N

        bidSpinner.setName("bidSpinner"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(auctionIdLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(productLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(veranstalterLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bieterLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gebotLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mengeLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(bidSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addComponent(timeLabel))))
                .addGap(60, 60, 60))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(auctionIdLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(productLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(mengeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(veranstalterLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(bieterLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(gebotLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(timeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bidSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addContainerGap())
        );

        bidButton.setText(resourceMap.getString("bidButton.text")); // NOI18N
        bidButton.setName("bidButton"); // NOI18N
        bidButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bidButtonActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bidButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bidButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Detail View eines Auktion
     * @param evt
     */
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        AuctionProdukt p = auctionModel.getRow(this.jTable1.getSelectedRow());
        Logger.trace("Lade Auktion für DetailView");

        binder.BindTo(auctionIdLabel, Integer.toString(p.getAuctionid()));
        binder.BindTo(productLabel, p.getProduct());
        binder.BindTo(mengeLabel, Integer.toString(p.getAmount()));
        binder.BindTo(veranstalterLabel, p.getPerson());
        if(p.getHighest_bidder() == null){
            binder.BindTo(bieterLabel, "kein höchster Bieter");
        }
        else{
            binder.BindTo(bieterLabel, p.getHighest_bidder());
        }
        
        binder.BindTo(gebotLabel, Integer.toString(p.getCurrent_bid()));
        binder.BindTo(timeLabel, p.getTtl());

        //aktuelles gebot in den Spinne rschreiben
        //so kann man das gebot leichter erhöhen
        this.bidSpinner.setValue(p.getCurrent_bid());

        //this.auctionIdLabel.setText(Integer.toString(p.getAuctionid()));
        //this.productLabel.setText(p.getProduct());
        //this.mengeLabel.setText(Integer.toString(p.getAmount()));
        //this.veranstalterLabel.setText(p.getPerson());
        //this.bieterLabel.setText(p.getHighest_bidder());
        //this.gebotLabel.setText(Integer.toString(p.getCurrent_bid()));
        //this.timeLabel.setText(p.getTtl());
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * ein gebot abgeben
     * @param evt
     */
    private void bidButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bidButtonActionPerformed
        int amount = Integer.parseInt(this.bidSpinner.getValue().toString());
        AuctionProdukt ap = auctionModel.getRow(this.jTable1.getSelectedRow());

        try{
            //ist gebor grösser null??
            if(amount <= 0){
                Logger.user("Sie müssen mehr als 0 Credits bieten!");
            }
            //gebot kleiner als aktuelles gebot??
            else if (amount <= ap.getCurrent_bid()){
                Logger.user("Gebot muss grösser als aktuelles Gebot sein!");
            }
            else{
                int i = BusinessLayer.bid(ap.getAuctionid(), amount);
                Logger.debug("User setzt Gebot ab");
                switch(i){
                    case 1:
                        Logger.user("Sie haben für " + ap.getProduct() + " geboten");
                        BindTo();
                        break;
                    case -9012:
                        Logger.user("Gebot ist zu niedrig");
                        break;
                    case -9013:
                        Logger.user("Auktion " + Integer.toString(ap.getAuctionid()) + " ist bereits Abgelaufen");
                        break;
                    case -9014:
                        Logger.user("Nicht genug Geld um fuer " + ap.getProduct() + " zu bieten");
                        break;
                    case -9999:
                        Logger.user("-9999 - Fehler - Others in der DB");
                        break;
                    default:
                        Logger.user("Etwas Unerwartetes ist geschehen!");
                        break;
                }
                
            }

        } catch(ArrayIndexOutOfBoundsException ex){
            Logger.user("Bitte ein Produkt auswählen");
        }

        
    }//GEN-LAST:event_bidButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        BusinessLayer.refresh();
        WiSiView.refreshAll();
        Logger.debug("Auction - REFRESCHT alle auktionen-------------");
        Logger.user("Auktionen Refrescht");
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel auctionIdLabel;
    private javax.swing.JButton bidButton;
    private javax.swing.JSpinner bidSpinner;
    private javax.swing.JLabel bieterLabel;
    private javax.swing.JLabel gebotLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel mengeLabel;
    private javax.swing.JLabel productLabel;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JLabel veranstalterLabel;
    // End of variables declaration//GEN-END:variables

}
